package com.liyy.platform.other.Provider_Consumer;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class Provider implements Runnable{
	//共享缓存区
	private BlockingQueue<Integer> queue;
	//多线程间是否启动变量，volatile声明的变量有强制从主内存中刷新的功能。即时返回线程的状态
	private volatile boolean isRunning = true;
	//id生成器
	private static AtomicInteger count=new AtomicInteger();
	
	public Provider(BlockingQueue<Integer> queue) {
		this.queue=queue;
	}

	@Override
	public void run() {
		try {
			while(isRunning){
				//休眠1000 毫秒 表示获取数据(产生数据的耗时)
				Thread.sleep(500);
				//获取的数据进行累计...
				int id = count.incrementAndGet();
				queue.put(id);
				System.out.println("当前线程:" + Thread.currentThread().getName() + "生产："+id);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	public void stop(){
		this.isRunning = false;
	}

}
